package com.tianqu.tms.www.cardect.controller;

import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.entity.TmsDetectNormal;
import com.tianqu.tms.entity.TmsDetectPic;
/**
 * 检测模式切换Controller
 * 
 * @author tianshifu
 *
 */
public class CarWDPersonNormalToSeniorDetectResultController extends
		TmsController {
	private static final String CARSERIAL = "carSerial";//车辆ID
		
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		//添加事务提交异常捕获
		try {
			controller.begin();

			String carSerial = (String)uc.getAttribute(CARSERIAL);//车辆ID
			
			//不要要查询数据库  直接清空uc就可以  清空uc的时候将车辆ID保留就可以了
			//需要进入数据库进行查询看是否有检测信息,有的话清空
			TmsDetectInfo carInfo = TmsDetectInfo.getInstance(); 
			carInfo = carInfo.getWDIpadCarInfoBySerial(carSerial,1);
			TmsDetectNormal normal = TmsDetectNormal.getInstance();
			TmsDetectPic pic = TmsDetectPic.getInstance();
			if(carInfo.getCarDetectId()>0)
			{
				//说明有检测报告，删之(普通检测报告)
				normal.setId(carInfo.getCarDetectId());
				normal.delDetectNormal(normal);
				//删除图片信息先查再删
				List<TmsDetectPic> picList = pic.findDetectPicInfoByDetectId(carInfo.getCarDetectId());
				
				if(picList!=null)
				{
					pic.delPic(carInfo.getCarDetectId());//删除所有检测图片信息
				}
				TmsUserProfile profile = uc.getUserProfile();
				//更新基本信息表
				carInfo.resetCarInfoByIdAndStatus(carSerial,3,profile.getUniqueId());
			}
			
			uc.clear();
			uc.setAttribute(CARSERIAL, carSerial);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR,e1);
			}
			//系统异常
			if(e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR,e);
		}

	}

}
